package com.glorysoft.wangnianxinpda.activity;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;

import com.glorysoft.wangnianxinpda.R;
import com.glorysoft.wangnianxinpda.base.App;
import com.glorysoft.wangnianxinpda.base.BaseActivity;
import com.glorysoft.wangnianxinpda.base.Constant;
import com.glorysoft.wangnianxinpda.databinding.ActivityUpdatePwdBinding;
import com.glorysoft.wangnianxinpda.entity.LoginEntity;
import com.glorysoft.wangnianxinpda.entity.net.ResponseEntity;
import com.glorysoft.wangnianxinpda.utils.MyUtils;
import com.glorysoft.wangnianxinpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ActivityManager;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import static com.glorysoft.wangnianxinpda.base.Constant.loginEntity;

public class UpdatePwdActivity extends BaseActivity {

    private ActivityUpdatePwdBinding binding = null;
    private String sOldPwd = "";
    private String sNewPwd = "";
    @Override
    protected int getLayoutResId() {
        return super.getLayoutResId();
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_update_pwd);

    }

    @Override
    protected void initData() {
        binding.setLoginEntity(Constant.loginEntity);

        MyUtils.setNewEditTextListener(binding.etOldPassword, this, s ->{
            sOldPwd = s;
        });

        MyUtils.setNewEditTextListener(binding.etNewPassword, this, s ->{
            sNewPwd = s;
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle(getString(R.string.update_pwd));
        freeUI_titleView.setRightListener(getString(R.string.confirmUpdate), v ->{
            updatePwd();//修改密码
        });
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), binding.llMain);
    }

    private void updatePwd() {
        if (sOldPwd.equals("")){
            showToast(getString(R.string.oldPWd_is_can_not_null), false);
            return;
        }
        if (sNewPwd.equals("")){
            showToast(getString(R.string.newPWd_is_can_not_null), false);
            return;
        }
        if (sNewPwd.equals(sOldPwd)){
            showToast(getString(R.string.newPwd_is_not_equals_oldPwd), false);
            return;
        }
        changeEPSD();

        //region 测试
//        showMessageDialog("修改成功", getSupportFragmentManager());
        //endregion
    }

    //消息提示框
    private void showMessageDialog(String message, FragmentManager fragmentManager) {
        FreeApi_DialogUtils.getConfirmDialog(
                App.app, "确定", message, () ->{
                    FreeApi_ActivityManager.getActivityManager().finishActivity();
                    FreeApi_Utils.skipWithExtraIntent("", this, LoginActivity.class);
                }
        ).show(fragmentManager, "DIALOG");

    }

    //region 接口调用
    //修改密码
    private void changeEPSD(){
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "LOGIN");
        map.addProperty("WORK_NO", loginEntity.getWORK_NO());
        map.addProperty("PASSWORD", sNewPwd);
        map.addProperty("OLDPASSWORD", sOldPwd);
        String method = "PDAChangePSD";
        NetUtils.requestPostNet("PDAChangePSDRequest", method, this, map, new FreeUI_DialogEntityCallBack<ResponseEntity<LoginEntity>>(
                new TypeToken<ResponseEntity<LoginEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseEntity<LoginEntity>> response) {
                if (response.body().isSuccess(false, getSupportFragmentManager())) {
                    showMessageDialog(response.body().getMsg(), getSupportFragmentManager());
                }
            }

            @Override
            public void onError(Response<ResponseEntity<LoginEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDAChangePSDRequest");
            }
        });
    }

    //endregion
}